package com.mazaiting.redeye.module.friend.add

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import cn.jpush.im.android.api.ContactManager
import cn.jpush.im.api.BasicCallback
import com.mazaiting.easy.app.IApplicationComponent
import com.mazaiting.easy.base.activity.BaseLoadingActivity
import com.mazaiting.log.L
import com.mazaiting.redeye.R
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.base.Config
import kotlinx.android.synthetic.main.activity_add_friend_passive.*

/**
 * 被动添加朋友页面
 */
class AddFriendPassiveActivity : BaseLoadingActivity<AddFriendPassivePresenter>(), AddFriendPassiveContract.View {

    companion object {
        /**
         * 用户名键
         */
        private const val KEY_USER_NAME = "key_user_name"
        /**
         * 理由键
         */
        private const val KEY_REASON = "key_reason"

        /**
         * 获取延时意图
         * @param context 上下文
         * @param username 对方名称
         * @param reason 理由
         */
        fun getPendingIntent(context: Context, username: String, reason: String): Intent {
            val intent = Intent(context, AddFriendPassiveActivity::class.java)
            intent.putExtra(KEY_USER_NAME, username)
            intent.putExtra(KEY_REASON, reason)
            return intent
        }
    }

    /**
     * 用户名
     */
    private var mUsername = ""

    override fun getContentLayout(): Int = R.layout.activity_add_friend_passive

    override fun inject(applicationComponent: IApplicationComponent?) {
        DaggerAddFriendPassiveComponent
            .builder()
            .applicationComponentImpl(applicationComponent as ApplicationComponentImpl)
            .build()
            .inject(this)
    }

    override fun bindView(view: View?, savedInstanceState: Bundle?) {
        super.bindView(view, savedInstanceState)
        initView()
    }

    /**
     * 初始化 View
     */
    private fun initView() {
        // 接受
        add_friend_btn_accept.setOnClickListener {
            ContactManager.acceptInvitation(mUsername, Config.JPUSH_KEY, object : BasicCallback(){
                override fun gotResult(code: Int, msg: String?) {
                    L.d("code: $code, msg: $msg")
                    if (0 == code) {
                        Toast.makeText(this@AddFriendPassiveActivity, "添加成功", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(this@AddFriendPassiveActivity, "添加失败", Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
        // 拒绝
        add_friend_btn_decline.setOnClickListener {
            ContactManager.declineInvitation(mUsername, Config.JPUSH_KEY, "太丑了...", object : BasicCallback(){
                override fun gotResult(code: Int, msg: String?) {
                    L.d("code: $code, msg: $msg")
                    if (0 == code) {
                        Toast.makeText(this@AddFriendPassiveActivity, "拒绝成功", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(this@AddFriendPassiveActivity, "拒绝失败", Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
    }

    override fun initData() {
        // 获取用户名
        mUsername = intent.getStringExtra(KEY_USER_NAME)
        // 获取原因
        val reason = intent.getStringExtra(KEY_REASON)
        // 设置用户名
        add_friend_username.text = mUsername
        // 设置理由
        add_friend_reason.text = reason
    }

}
